﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _3D
{
    class Bloco : GameObject3D
    {
        VertexPositionColor[] verts;
        VertexBuffer vertexBuffer;
        BasicEffect effect;
    
        public Bloco(float size, GraphicsDevice graphics, Camera camera, Vector3 position)
        {
            verts = new VertexPositionColor[14];
            verts[0] = new VertexPositionColor(new Vector3(-size + position.X, size + position.Y, size + position.Z), Color.White);
            verts[1] = new VertexPositionColor(new Vector3(size + position.X, size + position.Y, size + position.Z), Color.AntiqueWhite);
            verts[2] = new VertexPositionColor(new Vector3(-size + position.X, -size + position.Y, size + position.Z), Color.White);
            verts[3] = new VertexPositionColor(new Vector3(size + position.X, -size + position.Y, size + position.Z), Color.White);
            verts[4] = new VertexPositionColor(new Vector3(size + position.X, -size + position.Y, -size + position.Z), Color.Gray);
            verts[5] = new VertexPositionColor(new Vector3(size + position.X, size + position.Y, size + position.Z), Color.White);
            verts[6] = new VertexPositionColor(new Vector3(size + position.X, size + position.Y, -size + position.Z), Color.White);
            verts[7] = new VertexPositionColor(new Vector3(-size + position.X, size + position.Y, size + position.Z), Color.White);
            verts[8] = new VertexPositionColor(new Vector3(-size + position.X, size + position.Y, -size + position.Z), Color.White);
            verts[9] = new VertexPositionColor(new Vector3(-size + position.X, -size + position.Y, size + position.Z), Color.White);
            verts[10] = new VertexPositionColor(new Vector3(-size + position.X, -size + position.Y, -size + position.Z), Color.White);
            verts[11] = new VertexPositionColor(new Vector3(size + position.X, -size + position.Y, -size + position.Z), Color.White);
            verts[12] = new VertexPositionColor(new Vector3(-size + position.X, size + position.Y, -size + position.Z), Color.White);
            verts[13] = new VertexPositionColor(new Vector3(size + position.X, size + position.Y, -size + position.Z), Color.White);

            vertexBuffer = new VertexBuffer(graphics, typeof(VertexPositionColor), verts.Length, BufferUsage.None);
            vertexBuffer.SetData<VertexPositionColor>(verts);

            effect = new BasicEffect(graphics);
            effect.VertexColorEnabled = true;
            bBox = new BoundingBox(new Vector3(position.X - size,position.Y - size,position.Z - size), 
                                        new Vector3(size + position.X,size + position.Y,size + position.Z));
        }
        public override void Update(Camera camera)
        {
            effect.View = camera.view;
            effect.Projection = camera.projection;
            effect.World = Matrix.Identity;

            base.Update(camera);
        }
        public override void Draw(GraphicsDevice graphics)
        {
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                graphics.SetVertexBuffer(vertexBuffer);
                graphics.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, verts, 0, 12);
            }

            base.Draw(graphics);
        }
    }
}
